/*
 * Copyright 2005 Lumis EIP Tecnologia da Informação
 */

package lumis.content.source.field;

import lumis.content.table.ContentQueryBuilder;
import lumis.doui.source.ISourceData;
import lumis.doui.source.field.type.PrincipalDataType;
import lumis.doui.source.field.type.SourceFieldTypes;
import lumis.doui.table.field.TableSourceFieldMapping;
import lumis.portal.PortalException;
import lumis.util.query.QueryBase;
import lumis.util.query.QueryField;
import lumis.util.query.QueryFieldTypes;
import lumis.util.query.QueryTable;

/**
 * A workflow's assigned to source field.
 * 
 * @version $Revision: 6251 $ $Date: 2007-03-27 10:49:29 -0300 (Tue, 27 Mar 2007) $
 * @since 4.0.4
 */
public class WorkflowAssignedToSourceField extends MetaDataSourceField
{
	public WorkflowAssignedToSourceField() throws PortalException
	{
		super(ContentQueryBuilder.WORKFLOW_META_DATA_RELATION_ID, "assignedTo", SourceFieldTypes.LUMIS_PRINCIPAL);
	}

	@Override
	protected QueryField getQueryField(QueryBase queryBase) throws PortalException
	{
		QueryField queryField = super.getQueryField(queryBase);
		queryField.setDataType(QueryFieldTypes.STRING);
		return queryField;
	}

	@Override
	public TableSourceFieldMapping getTableMapping(QueryBase queryBase) throws PortalException
	{
		String fieldId = getId();
		TableSourceFieldMapping mapping = super.getTableMapping(queryBase);
		
		// add join with table lum_Principal
		QueryTable workflowMetaDataTable = queryBase.getTableByRelationId(ContentQueryBuilder.WORKFLOW_META_DATA_RELATION_ID);
		QueryTable principalTable = mapping.addTable("lum_Principal", "principalId", workflowMetaDataTable, "assignedTo");
		
		// add table lum_Principal fields
		QueryField principalId = principalTable.getField("principalId");
		principalId.setFieldAlias(fieldId+"_id");
		mapping.addField(principalId);
		QueryField shortIdField = principalTable.getField("shortId");
		shortIdField.setFieldAlias(fieldId+"_sid");
		mapping.addField(shortIdField);
		QueryField nameField = principalTable.getField("name");
		nameField.setFieldAlias(fieldId+"_name");
		mapping.addField(nameField);
		QueryField typeField = principalTable.getField("type");
		typeField.setFieldAlias(fieldId+"_type");
		typeField.setDataType(QueryFieldTypes.INTEGER);
		mapping.addField(typeField);
		QueryField subtypeField = principalTable.getField("subtype");
		subtypeField.setFieldAlias(fieldId+"_stype");
		subtypeField.setDataType(QueryFieldTypes.INTEGER);
		mapping.addField(subtypeField);
		
		this.lookupQueryField = nameField;
		return mapping;
	}
	
	@Override
	public void convertRowToFieldData(ISourceData databaseRow, ISourceData sourceRow)
	{
		String fieldId = getId();
		String principalId = (String)databaseRow.get(fieldId + "_id");
		if (principalId != null)
		{
			String shortId = (String)databaseRow.get(fieldId + "_sid");
			String name = (String)databaseRow.get(fieldId + "_name");
			int type = ((Number)databaseRow.get(fieldId + "_type")).intValue();
			int subtype = ((Number)databaseRow.get(fieldId + "_stype")).intValue();
			PrincipalDataType.Data data = new PrincipalDataType.Data(principalId, shortId, name, type, subtype);			
			sourceRow.put(fieldId, data);
		}
	}
}
